package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.entity.Egg;
import com.antdesignbackend.backend.entity.entityVo.EggVo;
import com.antdesignbackend.backend.service.EggService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("/egg")
@Controller
public class EggController extends BaseController {

    @Autowired
    EggService eggService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private Integer menuId;

    @GetMapping("/index")
    @AuthToken
    public String ProductionIndex(String menuId) {
        this.menuId = Integer.parseInt(menuId);
        return "records/egg/index";
    }

    /**
     * 分页查询
     * @param
     * @return
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    @AuthToken
    public ReturnVO tableSearch(@RequestBody EggVo eggVo) {
        ReturnVO result = new ReturnVO();
        List<EggVo> datalist=new ArrayList<>();
        try {
            QueryWrapper<EggVo> queryWrapper = new QueryWrapper<>();
            if (eggVo.getAnimalType() != null && !eggVo.getAnimalType().equals("")){
                queryWrapper.like("animalType",eggVo.getAnimalType());
            }
            if (eggVo.getTimeInterval() != null && !eggVo.getTimeInterval().equals("")){
                String[] str=eggVo.getTimeInterval().split(" - ");
                String star=str[0];
                String end=str[1];
                queryWrapper.between("eggTime",star,end);
            }
            queryWrapper.orderByDesc("eggTime");
            queryWrapper.eq("a.eid",getLoginEnterpriseId());
            //先统计满足条件的数据总数，因为sql 不包括排序
            int total=eggService.count(queryWrapper);
            result.setTotal(total);
            //再查询
            if(total>0)
            {
                datalist= eggService.search(queryWrapper,new Page<>(eggVo.getPageIndex(),eggVo.getPageSize()));
            }
            result.setData(datalist);
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return result;
    }
    /**
     * 修改/新增操作的保存
     */
    @PostMapping("/save")
    @ResponseBody
    @AuthToken
    public ReturnVO save(@RequestBody Egg egg) throws Exception {
        ReturnVO result = new ReturnVO();
        boolean b = false;
        if(egg.getEggId()==null || egg.getEggId()==0){
            egg.setUid(getLoginUserId());
            egg.setCreateTime(new Date());
            egg.setEid(getLoginEnterpriseId());
            b=eggService.save(egg);
        }else{
            b=eggService.updateById(egg);
        }
        if(b==true){
            result.setMessage("保存成功！");
            result.setCode("200");
        }
        else
        {
            result.setMessage("保存失败！");
            result.setCode("500");
        }
        return result;
    }
    /**
     * 删除
     * @param
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @ResponseBody
    public ReturnVO deleteById(@PathVariable Serializable id) {
        ReturnVO result = new ReturnVO();
        if (eggService.removeById(Integer.parseInt(id.toString())) == true) {
            result.setMessage("删除成功");
        } else {
            result.setMessage("删除失败");
            result.setCode("0");
        }
        return result;
    }
}
